package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class ClazzServiceimp implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;


    @Override
    public PageResult<Clazz> Page(ClazzQueryParam clazzQueryParam) {
        //分页查询
        PageHelper.startPage(clazzQueryParam.getPage(), clazzQueryParam.getPageSize());
        //跟据条件查询
        List<Clazz> clazzlist = clazzMapper.page(clazzQueryParam);
        //比较日期
        clazzlist.forEach(
                clazz ->
                {if(clazz.getEndDate().isBefore(LocalDate.now()))
                    clazz.setStatus("已结课");
                    else if(clazz.getBeginDate().isAfter(LocalDate.now()))
                {
                    clazz.setStatus("未开班");
                }
                        else clazz.setStatus(" 在读中");
                }
        );
        Page<Clazz> c = (Page<Clazz>) clazzlist;

        return new PageResult<Clazz>(c.getTotal(), c.getResult());
    }

    @Override
    public void insert(Clazz clazz) {
        clazz.setBeginDate(LocalDate.now());
        clazz.setEndDate(LocalDate.now());
        clazzMapper.insert(clazz);
    }

    @Override
    public void deleteById(Integer id) {
        clazzMapper.delete(id);

    }

    @Override
    public Clazz getById(Integer id) {
        Clazz clazz = clazzMapper.getById(id);
        return clazz;
    }

    @Override
    public void updateById(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateById(clazz);
    }

    @Override
    public List<Clazz> selectAll() {
        return clazzMapper.list();
    }


}
